// pages/user/user/userRecycling/userRecycling.js
const app = getApp();
var util = require('../../../../utils/util.js');
import Api from '../../../../utils/http'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currMonth: '本月',
    chooseMonth: '', //选中的月份
    endMonth: util.formatMonth(new Date), //限制不可选取未来的月份
    dataList: [],
    userInfo:'',//个人信息
    page:1,//页码
    pageSize:9,//显示条数
    code:null,//页面显示判断

  },

  //选择月份
  bindMonthChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var chooseMonth = e.detail.value;
    if (chooseMonth == util.formatMonth(new Date)) {
      chooseMonth = '本月'
    }
    this.setData({
      currMonth: chooseMonth
    })
    this.getsearch(e.detail.value);
  },
  //跳转回收袋领取
  toMyRecyclingGet: function () {
    var that=this;
    console.log(that.data)
    let uid=wx.getStorageSync('userid');
    if (uid.user_type==1 || uid.user_type==2) {
      if (that.data.userInfo.recyc_bag_num==0 || that.data.userInfo.recyc_bag_num==null) {
        wx.showToast({
          title: '数量不足，请先购买',
          icon:'none',
          duration:2000
        })
      }else{
        wx.navigateTo({
          url: '/pages/user/user/userRecyclingGet/userRecyclingGet',
        })
      }
    }else{
      wx.showToast({
        title: '当前可领取数量不足',
        icon:'none',
        duration:2000
      })
    }
    
  },
  //跳转回收袋购买
  toMyRecyclingBuy: function () {
    this.getVerification();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getReceive();
    this.getuserInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getReceive();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getReceiveList();
  },
  //搜索
  getsearch: function (value) {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      selType: 1,
      uid:uid.id,
      user_type: uid.user_type,
      pageNum: 1,
      pageSize: 9,
      time:value,
    }
    console.log(param)
    Api.ajax('/app/Zmanage/receiveList', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          dataList: res.data,
          code:res.code,
        })
      }else{
        that.setData({
          dataList: res.data,
          code:res.code,
        })
      }
    })
  },
  //判断验证
  getVerification: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
    };
    Api.ajax("/app/Zpersonalcenter/userInfo", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        if (res.data.verification!=1) {
          wx.showModal({
            title:'提示',
            content:'当前用户未认证，请认证后在购买',
            showCancel:'true',
            success (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/user/user/mobileAuth/mobileAuth',
                })
              }
            }
          })
        }else{
          wx.navigateTo({
            url: '/pages/user/user/userRecyclingBuy/userRecyclingBuy',
          })
        }
        
      }
    })
  },
  //个人信息
  getuserInfo: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
    };
    Api.ajax("/app/Zpersonalcenter/userInfo", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        
        that.setData({
          userInfo: res.data
        })
      }
    })
  },
  //下拉刷新
  getReceive: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      selType: 1,
      uid:uid.id,
      user_type: uid.user_type,
      pageNum: 1,
      pageSize: 9,
    }
    console.log(param)
    Api.ajax('/app/Zmanage/receiveList', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          dataList: res.data,
          code:res.code,
          page:2
        })
      }
    })
  },
  //我的回收袋列表
  getReceiveList: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var page = that.data.page;
    var dataList = that.data.dataList;
    var param = {
      selType: 1,
      uid:uid.id,
      user_type: uid.user_type,
      pageNum: page,
      pageSize: that.data.pageSize,
    }
    console.log(param)
    Api.ajax('/app/Zmanage/receiveList', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        page++;
        that.setData({
          dataList: dataList.concat(res.data),
          code:200
        })
      }else{
        wx.showToast({
          icon: 'none',
          title: '暂时没有数据哦！',
        })
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})